package com.bsj.power.common.config.cached;

import com.alibaba.fastjson2.JSON;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.def.entity.locationDevice.LocationDevice;
import com.bsj.power.common.def.entity.path.Path;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import redis.clients.bsj.JedisClusterBSJ;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description LocationDeviceCached
 * @time 2024/4/19 14:54
 */
@Component
public class LocationDeviceCached {

    @Autowired
    private JedisClusterBSJ jedisClusterBSJ;

    /**
     * 获取定位设备信息
     *
     * @param deviceName
     * @return com.bsj.power.common.def.entity.locationDevice.LocationDevice
     * @author ljx
     * @time 2024/5/8 17:31
     */
    public LocationDevice hgetLocationDeviceInfo(String deviceName) {
        String locationDeviceStr = jedisClusterBSJ.hget(RedisConstant.LOCATION_DEVICE_REDIS_KEY, deviceName);
        if (locationDeviceStr == null) {
            return null;
        }
        return JSON.parseObject(locationDeviceStr, LocationDevice.class);
    }

    /**
     * 获取所有定位设备信息
     *
     * @return java.util.List<com.bsj.power.common.def.entity.locationDevice.LocationDevice>
     * @author ljx
     * @time 2024/6/8 9:58
     */
    public List<LocationDevice> hgetAllLocationDeviceInfo() {
        Map<String, String> map = jedisClusterBSJ.hgetAll(RedisConstant.LOCATION_DEVICE_REDIS_KEY);
        if (CollectionUtils.isEmpty(map)) {
            return null;
        }
        return map.entrySet().stream().map(value -> {
            return JSON.parseObject(value.getValue(), LocationDevice.class);
        }).collect(Collectors.toList());
    }

    /**
     * 缓存定位设备信息
     *
     * @param locationDevice
     * @return void
     * @author ljx
     * @time 2024/5/8 17:31
     */
    public void hsetLocationDeviceInfo(LocationDevice locationDevice) {
        if (locationDevice != null) {
            jedisClusterBSJ.hset(RedisConstant.LOCATION_DEVICE_REDIS_KEY, locationDevice.getDeviceName(), JSON.toJSONString(locationDevice));
        }
    }

    /**
     * 删除定位设备信息
     *
     * @param deviceName
     * @return void
     * @author ljx
     * @time 2024/5/8 17:32
     */
    public void hdelLocationDeviceInfo(String deviceName) {
        jedisClusterBSJ.hdel(RedisConstant.LOCATION_DEVICE_REDIS_KEY, deviceName);
    }

}
